using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;

//首板，往前推一个月以上，有一根阳柱，量能明显高出其他阳柱，并且当天没有涨停，且收阳线。那么首板当天的价格只要超过立桩量那天的收盘价，就买入
public class LizhuangliangAnalysis : BatchProcessor
{
    public int fromYear => AppController.instance.StockDataAnalysis.fromYear;
  
    protected override void onCompleted()
    {
        if (m_pickedStocks != null)
        {
            string content = JsonConvert.SerializeObject(m_pickedStocks);
            Debug.LogError($"选出的立桩量股票有：{content}");
            onEnd?.Invoke(content);
        }
    }
    StockData FindLizhuangliangShouban(StockAllData stock)
    {
        Debug.Log($"FindLizhuangliangShouban:{stock.stockData.dm}");

         var stockAllData = StocksDataController.instance.GetStockAllData(stock.stockData.dm, fromYear);
        if (stockAllData == null || stockAllData.stockTransitions == null)
            return null;

        var yijianfenghou = new StockTransitionsRanges();
        yijianfenghou.stockCode = stock.stockData.dm;
        yijianfenghou.transitionsRanges = new List<TransitionsRange>();
        
        var transitions = stockAllData.stockTransitions;

        if (transitions.Count < 4)
            return null;

        var cur = transitions[^1];
        var pre = transitions[^2];
        var qian = transitions[^3];
        var qian2 = transitions[^4];

        int shouban = 0;
        //首板
        bool goUpTop = (cur.c - pre.c) / pre.c > 0.096f;
        if (!goUpTop)
        {
            return null;
        }
        bool preGoUpTop2 = (qian.c - qian2.c) / qian2.c > 0.096f;
        if (preGoUpTop2)
        {
            //Debug.Log($"{stock.stockData.dm} 非首板");
            return null;
        }
        shouban = transitions.Count - 1;
        bool preGoUpTop = (pre.c - qian.c) / qian.c > 0.096f;
        if(preGoUpTop)
        {
            shouban = transitions.Count - 2;
        }

        int lizhuang = 0;
        //往前推一个月以上，有一根阳柱，量能明显高出其他阳柱，并且当天没有涨停，且收阳线
        double chengjiaoTotal = 0;
        int count = 0;
        for (int i = transitions.Count-50; i <= transitions.Count-4; i++)
        {
            if (i < 0) 
                continue;

            count++;
            chengjiaoTotal +=  transitions[i].v;
        }
        double averChengjiao = chengjiaoTotal / count;
        
        for (int i = transitions.Count-50; i <= transitions.Count-4; i++)
        {
            if (i < 0) 
                continue;

            if (transitions[i].v > averChengjiao * 2)
            {
                int lizhuangpre = i - 1;
                if (lizhuangpre < 0) 
                    continue;

                double goupRate = (transitions[i].c - transitions[lizhuangpre].c) / transitions[lizhuangpre].c;
                if (goupRate < 0 || goupRate > 0.096f)
                {
                    Debug.Log($"{stock.stockData.dm} 巨量日 涨停或下跌:{goupRate}");
                    return null;
                }
                
                lizhuang = i;
            }
        }

        if (lizhuang <= 0)
        {
            return null;
        } 
        
        //首板当天的价格只要超过立桩量那天的收盘价,就买入
        if (transitions[shouban].c < transitions[lizhuang].c)
        {
            Debug.Log($"首板没过立桩量的收盘价");
            return null;
        }
        return stock.stockData;
    }
    
    [ContextMenu("选高于立桩量首板股")]
    public void PickAllLizhuangliang()
    {
        actionInterval = 0.01f;
        m_pickedStocks = new List<StockData>();
        DoActionToAllStocks(DoPickLizhuangliang);
    }

    private List<StockData> m_pickedStocks;
    async void DoPickLizhuangliang(StockData stock)
    {
        var stockAllData = StocksDataController.instance.GetStockAllData(stock.dm,fromYear);
        if (stockAllData == null || stockAllData.stockTransitions == null)
            return;
        
        var shouban = FindLizhuangliangShouban(stockAllData);
        if (shouban != null)
        {
            Debug.LogError($"选出立桩量首板股：{JsonConvert.SerializeObject(shouban)}");
            m_pickedStocks.Add(stock);
        }
    }
}